<?php
class UtilUploader {
	
	public static function PLupload()
	{

	}
	
	public function keyExchange(&$value, $key, $prefix='file_')
	{
		$value =  $prefix.$value;
		
		
	}
	
	
	/**
	 * 使用ext.upload.Muploadify.MUploadify
	 * 
	 * ************************************************************************************************
	 * @param string $name
	 * @param string $prefix
	 * @throws CHttpException
	 */
     
     public static function uploadifyCheck()
     {
		$fileArray = array();
		foreach ($_POST as $key => $value) {
			if ($key != 'folder') {
				if (file_exists($_SERVER['DOCUMENT_ROOT'] . $_POST['folder'] . '/' . $value)) {
					$fileArray[$key] = $value;
				}
			}
		}
		
		$result['fileArray'] = $fileArray;
		
		UtilHelper::writeToFile($result, 'a+');
		echo json_encode($fileArray);        
     }
	
	public static function uploadify($name, $prefix = 'file_' )
	{
		$result = array();
		
		$result['REQUEST'] = $_REQUEST;
		$result['FILES'] = $_FILES;
			
			
		try{
		
			$tempFile = $_FILES[$name]['tmp_name'];
		
			$folder = $_POST['folder'];
					
			//传递参数
			$data = json_decode($_REQUEST[$name]);
		
			$result[$name] = $data;
		
			$fileext = explode( ';', $_REQUEST['fileext']);
		
			$result['Ext'] = $fileext;
			
			$picture = CUploadedFile::getInstanceByName($name);
			
			$now = time();
			$dataArray = array(
					'pid'=>$data->id,
					'created'=>$now,
					'ext'=>strtolower($picture->getExtensionName()),
					'name'=>$picture->getName(),
					'size'=>$picture->getSize(),
					'mine'=>CFileHelper::getMimeType($picture->getName()),
					'links'=>md5(date('ymdhis', $now).$picture->getName()),
					'owner'=>Yii::app()->user->id,
					'isfolder'=>0,
					'hits'=>0,
					'type'=>Category::CATEGORY_MEDIA,
					'status'=>File::FILE_STATUS_PUBLISHED,
					'islocal'=>File::FILE_ISLOCAL
					
					);
			
			///重组数组dataArray 开始
			
			$keys = array_keys($dataArray);
			
			array_walk($keys, 'UtilUploader::keyExchange',$prefix);
			
			$values = array_values($dataArray);			
			
			$dataArray = array_combine($keys, $values);
			//重组数组dataArray结束
			
			$result['dataArray'] = $dataArray;
		
			$model = new File();
			
			$model->attributes = $dataArray;
			
			$file_links = $prefix.'links';
			$model->$file_links = $dataArray['file_links'];
			
			
			
// 			$model->file_pid =$data->id;
// 			$model->file_created = time();
// 			$model->file_ext = strtolower($picture->getExtensionName());
// 			$model->file_name = $picture->getName();
// 			$model->file_size = $picture->getSize();
// 			$model->file_mine = CFileHelper::getMimeType($picture->getName());
// 			$model->file_links = md5(date('ymdhis',$model->file_created).$picture->getName());
// 			$model->file_owner = Yii::app()->user->id;
// 			$model->file_isfolder = 0;
// 			$model->file_hits = 0;
// 			$model->file_type = Category::CATEGORY_MEDIA;
// 			$model->file_status = File::FILE_STATUS_PUBLISHED;
// 			$model->file_islocal = File::FILE_ISLOCAL;
		
			$result['Model'] = $model->attributes;
		
			$targetFile = File::model()->generateFilePath($model, true, false);
		
			$result['Path']=array(
					'tempName'=>$tempFile,
					'targetFile'=>$targetFile
			);
		
		
			UtilHelper::writeToFile($result, 'a+');
		
		
			if ($model->save())
			{
				$id = $model->file_id;
				//验证文件格式
				if (in_array(strtolower('*.'.$model->file_ext),$fileext))
				{
					// Uncomment the following line if you want to make the directory if it doesn't exist
					// mkdir(str_replace('//','/',$targetPath), 0755, true);
		
					if(!$picture->saveAs($targetFile))
						throw new CHttpException(500);
						
					$response = array(
							'state'=>'success',
							'name'=>$model->file_name,
							'path'=>$targetFile,
							'id'=>$model->file_id
					);
		
					echo json_encode($response);
		
					// 							echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
						
					$result['Response'] = $response;
				}
			}
				
		}catch(CException $e){
			$result['ERROR'] = $e->getMessage().$e->getTraceAsString();
				
		}
			
		UtilHelper::writeToFile($result, 'a+');
		
			
		Yii::app()->end();
	}
}

?>